<template>
  <!-- el-row一行 el-col两列 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 左侧内容 -->
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <!-- 右侧内容 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 放置下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="add"
                :disabled="!checkPermission('departments')"
                >添加子部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="edit"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="del"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { DeleteDepartments } from "@/api/departments";
export default {
  //需要对外开放属性
  props: {
    //定义传入的属性
    treeNode: {
      require: true,
      type: Object,
    },
    isRoot: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    //点击删除 新增 编辑触发的事件
    handleCommand(command) {
      if (command === "add") {
        // 添加子部门
        this.$emit("addDepts", this.treeNode);
      } else if (command === "edit") {
        // 编辑部门
        this.$emit("editDepts", this.treeNode); //点击谁编辑谁
      } else {
        // 删除部门
        this.$confirm("是否确认删除")
          .then(() => {
            return DeleteDepartments(this.treeNode.id);
          })
          .then(() => {
            //删除成功就会进入到这里
            //通知父组件删除数据成功，刷新下数据
            this.$emit("delDepts");
            this.$message.success("删除部门成功");
          });
      }
    },
  },
};
</script>
<style scope>
</style>